using System;
using Microsoft.SmallBasic.Library;
using Math = System.Math;

namespace SmallBasicFun.Quizes
{
	public class TreeQuiz : IQuiz
	{
		private  int xTop = 25;
		private  int yTop = 25;

		
		public  Question Question1DoubleLength
		{
			get { return Quiz.questions[0]; }
		}
		public  Question Question2DecreaseTurn
		{
			get { return Quiz.questions[1]; }
		}
		public  Question Question3SetNinety
		{
			get { return Quiz.questions[2]; }
		}
		public  Question Question4AngleFive
		{
			get { return Quiz.questions[3]; }
		}
		

		public  void StartTest()
		{
			Tortoise.SetSpeed(10);
			GradeResults();
			InitializeScreen();
			DrawSuccess();
			Quiz.DisplayScores(4,350);
		}

		private  void DrawSuccess()
		{
			DrawMaze();
			RunThruMaze();
		}

		private  void RunThruMaze()
		{
			Question1DoubleLength.SetVariable("length", 15);
			Question1DoubleLength.Run();
			Tortoise.SetSpeed(10);
			Tortoise.SetPenColor(Colors.Lime);
			Tortoise.SetPenWidth(2);

			var angles = Question1DoubleLength.GetVariable("angles");
			
			var turns = new [] {0,90,0,-90,90-36,-90,0,90,-90,0,0,0,-90,-90,0,0,90,0,0,90,0,0,0,0,90,90,-90,0,-90,90,-90};
			for (int i = 0; i < turns.Length; i++)
			{
				if(i == 4)
				{
					
					Question4AngleFive.Run();
					angles = Question1DoubleLength.GetVariable("angles");
					angles[i + 1] = angles[i+1] + turns[i];
					
					Question1DoubleLength.SetVariable("angles", angles);
				}
				else if (turns[i] != 90)
				{
					angles = Question1DoubleLength.GetVariable("angles");	
					angles[i + 1] = turns[i];
					
					Question1DoubleLength.SetVariable("angles", angles);
				}
				else
				{
					Question1DoubleLength.SetVariable("turn",i+1);
					Question3SetNinety.Run();
				}
				
			}
			Question1DoubleLength.SetVariable("turn", 0);
			angles = Question1DoubleLength.GetVariable("angles");
			
			for (int i = 0; i < turns.Length; i++)
			{
				Question2DecreaseTurn.Run();
				var index = Question1DoubleLength.GetVariable("turn");
				Tortoise.Turn(angles[Math.Abs(index)]);
				Tortoise.Move(Question1DoubleLength.GetVariable("length"));

			}	
		}

		private  void DrawMaze()
		{
			GraphicsWindow.PenColor = Colors.DarkGray;
			GraphicsWindow.PenWidth = 4;
			var verticalLines = new[] { new[] { 1, 4 }, new[] { 0, 2, 3, 1 }, new[] { 2, 1 }, new[] { 0, 1 }, new[] { 1, 3 }, new[] { 1, 2 }, new[] { 2, 1 }, new int[0], new[] { 1, 1, 3, 2 }, new[] { 2, 1 }, new[] { 0, 4 } };
			var horizontalLines = new[] { new[] { 0, 10 }, new[] { 2, 1, 5, 4 }, new[] { 1, 1, 3, 1, 7, 1 }, new[] {0,1, 2, 2, 5, 3 ,9,1}, new[] { 1, 2, 4, 3, 8, 1 }, new[] { 0, 10} };
			DrawLines(horizontalLines, (x, y, length) => GraphicsWindow.DrawLine(x, y, x + length, y));
			DrawLines(verticalLines, (y, x, length) => GraphicsWindow.DrawLine(x, y, x, y + length));
		}

		private  void DrawLines(int[][] lines, Action<int, int, int> action)
		{
			for (int line = 0; line < lines.Length; line++)
			{

				for (int i = 0; i < lines[line].Length; i += 2)
				{

					var grid = 30;
					var x1 = xTop + lines[line][i] * grid;
					var y = yTop + line * grid;
					var length = lines[line][i + 1] * grid;
					action(x1, y, length);
				}
			}
		}


		private  void InitializeScreen()
		{
			ProgramWindow.Clear();
			ProgramWindow.SetBackgroundColor(Colors.Black);

			Tortoise.SetOrientation(xTop - 15,yTop + 15,90);
			Tortoise.Show();
		}


		private  void GradeResults()
		{
			Grade1DoubleLength();
			Grade2DecreaseTurn();
			Grade3SetNinety();
			Grade4AngleFive();
		}

		private  void Grade1DoubleLength()
		{
			Question1DoubleLength.SetVariable("length", -6);
			Question1DoubleLength.Run();
			Question1DoubleLength.Result = -12 == Question1DoubleLength.GetVariable("length");
		}
		private  void Grade2DecreaseTurn()
		{
			Question2DecreaseTurn.SetVariable("turn", -6);
			Question2DecreaseTurn.Run();
			Question2DecreaseTurn.Result = -7 == Question1DoubleLength.GetVariable("turn");
		}
		private  void Grade3SetNinety()
		{
			Question3SetNinety.SetVariable("turn", -6);
			Question3SetNinety.Run();
			var value = Question1DoubleLength.GetVariable("angles")[-6];
			Question3SetNinety.Result = (90 == value);
		}
		private  void Grade4AngleFive()
		{
			Question4AngleFive.Run();
			Question4AngleFive.Result = 36 == Question1DoubleLength.GetVariable("angles")[5];
		}
	}
}